//
//  YHSelectBtnView.swift
//  SortProject
//
//  Created by Vast on 2022/7/23.
//

import UIKit

enum Select_Status: Int {
    case Phone = 101
    case User = 102
}
typealias ClickAction = (Select_Status) -> ()

class YHSelectBtnView: UIView {

    var titles: Array<String>?
    var selectStatus = Select_Status.Phone
    private var clickAction: ClickAction?
    convenience init(frame: CGRect, status: Select_Status = .User, titleArray: Array<String>, callback: @escaping ClickAction) {
        self.init(frame: frame)
        self.clickAction = callback
        self.titles = titleArray
        self.selectStatus = status
        uiConfig()
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func uiConfig() {
        self.backgroundColor = colorGray244
        self.layer.cornerRadius = 4
        self.layer.masksToBounds = true
        
        let phoneBtn = createButton(title: titles![0], action: #selector(clickBtn(btn:)))
        phoneBtn.frame = CGRect(x: 0, y: 0, width: Int(self.width) / 2, height: Int(self.height))
        self.addSubview(phoneBtn)
        let userBtn = createButton(title: titles![1], action: #selector(clickBtn(btn:)))
        userBtn.frame = CGRect(x: Int(phoneBtn.right), y: 0, width: Int(self.width) / 2, height: Int(self.height))
        self.addSubview(userBtn)
        phoneBtn.tag = 101
        userBtn.tag = 102
        if selectStatus == .Phone {
            phoneBtn.isSelected = true
            userBtn.isSelected = false
            phoneBtn.backgroundColor = colorTheme
            phoneBtn.backgroundColor = colorGray244
        } else {
            phoneBtn.isSelected = false
            userBtn.isSelected = true
            phoneBtn.backgroundColor = colorGray244
            userBtn.backgroundColor = colorTheme
        }
    }
    
    func createButton(title: String, action: Selector) -> UIButton {
        let btn = UIButton.init(type: .custom)
        btn.setTitle(title, for: .normal)
        btn.setTitleColor(colorNormalText, for: .normal)
        btn.setTitleColor(.white, for: .selected)
        btn.addTarget(self, action: action, for: .touchUpInside)
        btn.backgroundColor = colorGray244
        btn.layer.cornerRadius = 4
        btn.layer.masksToBounds = true
        return btn
    }
    
    @objc func clickBtn(btn: UIButton) {
        let tag = btn.tag
        if tag != selectStatus.rawValue {
            selectStatus = Select_Status(rawValue: tag)!
            
            let otherBtn = self.viewWithTag(tag == 101 ? 102 : 101) as! UIButton
            btn.isSelected = true
            btn.backgroundColor = colorTheme
            
            otherBtn.isSelected = false
            otherBtn.backgroundColor = colorGray244
            
            if let clickAction = clickAction {
                clickAction(selectStatus)
            }
        }
        
    }

}
